package pers.cz.message;

import cn.hutool.core.date.DateUtil;
import pers.cz.mapper.NotifyMessageMapper;
import pers.cz.model.NotifyMessage;
import pers.cz.spring.SpringContextUtil;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @program: PostGirl-panent
 * @description: MessageUtil
 * @author: Cheng Zhi
 * @create: 2021-10-08 15:04
 **/
public class MessageUtil {

    /**
     * 插入消息
     * @param content 内容
     * @param MessagType 消息类型
     * @param type 消息已读或者未读
     */
    public static void insertNotifyMessage(String content, String MessagType, String type, String userId) {

        NotifyMessage notifyMessage = new NotifyMessage();

        if (userId != null) {
            notifyMessage.setUserId(Integer.valueOf(userId));

        } else {
            notifyMessage.setUserId(Integer.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString()));

        }
        notifyMessage.setContent(content);
        notifyMessage.setMessageType(MessagType);
        notifyMessage.setType(type);
        String theme = "";
        if (content.length() > 30) {
            theme = content.substring(0,29);
        } else {
            theme = content;
        }

        notifyMessage.setTheme(theme); // 主题
        notifyMessage.setCreateTime(DateUtil.date());
        SpringContextUtil.getBean(NotifyMessageMapper.class).insert(notifyMessage);
    }

    public static void main(String[] args) {
        System.out.println(MessageType.getName("notify"));;
    }
}
